import { TaBase64 } from './script/encrypt'

// 定义内部跳转规则
export const goto = {

	// 指定跳转
	href(page: string, force: boolean = false) {
		return force ? uni.redirectTo({ url: page }) : uni.navigateTo({ url: page })
	},

	// 跳回上页
	back(): Promise<any> {
		let pages: any[] = getCurrentPages(), size: number = pages.length
		if (size > 0) for (let index: number = size; index > 0; index--) {
			if (pages[size - index].route !== pages[size - 1].route) {
				return uni.navigateBack({ delta: size - index })
			}
		}
		return this.home();
	},

	// 跳转首页
	home() {
		return uni.switchTab({ url: '/pages/home/index' })
	},

	// 跳转购物车
	cart() {
		return uni.switchTab({ url: '/pages/home/cart' })
	},

	// 跳转商品分类
	cate(id: string | number = 0) {
		// @ts-ignore
		id > 0 && (getApp().globalData.GoodsCateId = id)
		return uni.switchTab({ url: `/pages/home/cate?id=${id}` })
	},

	// 跳转绑定手机
	bind() {
		return uni.navigateTo({ url: '/pages/user/bind' })
	},

	// 跳转到搜索页
	search() {
		uni.navigateTo({
			url: "/pages/goods/search"
		})
	},

	// 跳转商品详情
	goods(code: string, hash: string = '') {
		return uni.navigateTo({ url: `/pages/goods/show?code=${code}&hash=${hash}` })
	},

	// 跳转商品详情
	store(code: string) {
		return uni.navigateTo({ url: `/pages/store/info?code=${code}` })
	},

	// 跳转订单列表或订单详情
	order(code: string | number, force: boolean = false) {
		if (typeof code === 'number') {
			return uni.navigateTo({ url: `/pages/order/index?type=${code}` })
		} else if (force) {
			return uni.redirectTo({ url: `/pages/order/show?code=${code}` })
		} else {
			return uni.navigateTo({ url: `/pages/order/show?code=${code}` })
		}
	},

	// 跳转订单支付页面
	payment(code: string, force: boolean = false) {
		if (force) {
			return uni.redirectTo({ url: `/pages/order/mpay?code=${code}` })
		} else {
			return uni.navigateTo({ url: `/pages/order/mpay?code=${code}` })
		}
	},

	// 邀请下级记录
	userSpread() {
		uni.navigateTo({ url: '/pages/user/center/spread' })
	},

	// 跳转用户海报
	userPoster() {
		uni.navigateTo({ url: '/pages/user/center/poster' })
	},

	// 跳转返利记录
	userRebate() {
		uni.navigateTo({ url: '/pages/user/center/rebate' })
	},

	// 跳转提现记录
	userTransfer() {
		uni.navigateTo({ url: '/pages/user/center/transfer' })
	},

	// 跳转余额列表
	userBalance() {
		uni.navigateTo({ url: '/pages/user/center/balance' })
	},

	// 跳转积分列表
	userIntegral() {
		uni.navigateTo({ url: '/pages/user/center/integral' })
	},

	// 等级升级
	userUpgrade() {
		uni.navigateTo({ url: '/pages/user/center/upgrade' })
	},

	// 用户收藏
	userCollect() {
		uni.navigateTo({ url: '/pages/user/action/collect' })
	},

	// 用户足迹
	userHistory() {
		uni.navigateTo({ url: '/pages/user/action/history' })
	},

	// 跳转到地址首页
	addrList(back: string = '', force: boolean = false) {
		if (force) {
			return uni.redirectTo({ url: `/pages/user/address/index?back=${back}` })
		} else {
			return uni.navigateTo({ url: `/pages/user/address/index?back=${back}` })
		}
	},

	// 跳转地址内容
	addrForm(id: number = 0, back: string = '', force: boolean = false) {
		if (force) {
			return uni.redirectTo({ url: `/pages/user/address/form?id=${id}&back=${back}` })
		} else {
			return uni.navigateTo({ url: `/pages/user/address/form?id=${id}&back=${back}` })
		}
	},

	// 选择收货地址
	addrSelect(uri: string) {
		let back: string = TaBase64.Safe.encrypt(uri);
		return uni.navigateTo({ url: `/pages/user/address/index?back=${back}` })
	},

	// 确认收货地址
	addrConfirm(back: string = '', addrid: number = 0) {
		let path: string = TaBase64.Safe.decrypt(back)
		return uni.redirectTo({ url: `${path}&addrid=${addrid}` })
	},
}